(defun doxy-all()
  (interactive)
  (message "Generating doxygen comments according to baidu coding style")
  (doxy-file)
)

(defun doxy-file()
  (interactive)
  (message "Generating doxygen comments for the file")
  (goto-char 1)
  (insert
   (format 
"  /**
 * @file %s
 * @author %s(%s@baidu.com)
 * @date %s
 * @version $Revision: $
 * @brief
 **/\n\n"
     (buffer-name) (user-login-name) (user-login-name)
     (format-time-string "%Y/%m/%d %I:%M:%S")))
)

(defun create-ns(namespace)
  (interactive "sNamespace:")
  (insert (format "namespace %s {\n\n}  // namespace %s"
                  namespace namespace))
)

(defun defp()
  (interactive)
  (setq original-point (point))
  (setq macro
        (replace-regexp-in-string "[/.\\ :]" "_" (concat (upcase (buffer-name)) "_")))
  (goto-char (point-min))
  (insert (format "#ifndef %s\n" macro))
  (insert (format "#define %s\n" macro))
  (goto-char (point-max))
  (insert "\n\n\n#endif\n")
  (goto-char original-point)
)

(defun doxy-func()
  "Generate doxygen comment for function"
  (interactive)
  (beginning-of-line)
  (setq original_pos (point))

  ;; Parse the function name
  (search-forward "(")
  (setq args-begin (point)) ;; first pos for args parsing
  (backward-char 1)
  (skip-chars-backward " \n")
  (skip-chars-backward "a-zA-Z0-9_~*")
  (setq func-name (thing-at-point 'symbol))
  (message "function name: %s" func-name)

  ;; Parse return type
  (skip-chars-backward " \n")
  (setq end (point))
  (beginning-of-line)
  (skip-chars-forward "\t ")
  (setq return-type (buffer-substring-no-properties (point) end))
  (message "return type: (%s)" return-type)

  ;; Get function declaration end point
  (re-search-forward "[;{]" nil t)
  (backward-char 1)
  (setq end-pos (point))

  (setq name-list '())
  (setq type-list '())

  ;; Parse arguments
  (goto-char args-begin)
  (while (search-forward "," end-pos t)
    (backward-char)
    (re-search-backward "[,(]" nil t)
    ;; Get type
    (skip-chars-forward " \n,(")
    ;;(read-char "parsing type")
    (setq type-list (cons (thing-at-point 'symbol) type-list))
    (message "Got type: %s" (thing-at-point 'symbol))

    ;; Get name
    (search-forward "," nil t)
    (backward-char)
    (skip-chars-backward " \n")
    (skip-chars-backward "a-zA-Z0-9_*")
    ;;(read-char "parsing name")
    (setq name-list (cons (thing-at-point 'symbol) name-list))
    (message "Got name: %s" (thing-at-point 'symbol))

    (search-forward "," nil t)
  )
  ;; Parse the last argument
  (goto-char original_pos)
  (search-forward ")")
  (re-search-backward "[,(]" nil t)
  (skip-chars-forward " \n,(")
  (message "last type is: ||%s||" (thing-at-point 'symbol))
  ;; Parse last type
  ;;(read-char "parsing last type")
  (message "last type: %s" (thing-at-point 'symbol))
  (if (not (null (thing-at-point 'symbol)))
      (progn
      (setq type-list (cons (thing-at-point 'symbol) type-list))
      ;; Parse last name
      (search-forward ")")
      (backward-char)
      (skip-chars-backward " \n")
      (skip-chars-backward "a-zA-Z0-9_*")
      ;;(read-char "parsing last name")
      (message "last name: %s" (thing-at-point 'symbol))
      (if (not (null(thing-at-point 'symbol))) (setq name-list (cons (thing-at-point 'symbol) name-list)))
      )
    )

  (message "final name-list len: %d" (length name-list))
  ;; Begin generating comments
  (goto-char original_pos)
  (insert "/**\n * @brief\n")
 
  (if (> (length name-list) 0)
      (progn (message "args len: %s"(length name-list)) (loop for pair in
        (loop for name in (reverse name-list)
        for type in (reverse type-list)
        collect (list type name)
        )
        do (insert (format " * @param [in] %s: \n" (nth 1 pair)))
      )))

  (insert (format " * @return \n**/\n" return-type))
  (c-indent-defun)
)